from json import loads

from flask import request

from handle.StudentHandle import stuSearch, stuDelete, stuAdd, stuCount, stuSearchAll, staticsCount
from publicHandle.publicHandle import returnData, searchUserId


class apiStudent():
    def __init__(self, app):

        # 查询所有学生信息
        @app.route("/allStu", methods=["POST"])
        def allStu():
            print(request.headers.get("userid"))
            if searchUserId(request.headers.get("userid")) :
                return stuSearchAll()
            else:
                return returnData({}, "请先登录", 401)


        # 查询具体学生信息
        @app.route('/searchStu', methods=["POST"])
        def searchStu():
            if searchUserId(request.headers.get("userid")) :
                number = request.values.get("number")
                result = stuSearch(number)
                print(result)
                return result
            else:
                return returnData({}, "请先登录", 401)


        # 删除一个学生
        @app.route('/deleteStu', methods=["POST"])
        def deleteStu():
            if searchUserId(request.headers.get("userid")) :
                number = request.values.get("number")
                return stuDelete(number)
            else:
                return returnData({}, "请先登录", 401)


        # 添加一个学生
        @app.route('/addStu', methods=["POST"])
        def addStu():
            if searchUserId(request.headers.get("userid")):
                data = loads(request.values.get("values"))
                number = data.get("number")
                name = data.get("name")
                sex = data.get("sex")
                age = data.get("age")
                grade = data.get("grade")
                profession = data.get("profession")
                credit = data.get("credit")
                place = data.get("place")
                return stuAdd(number, name, sex, age, grade, profession, credit, place)
            else:
                return returnData({}, "请先登录", 401)
        @app.route('/editStu', methods=["POST"])
        def editStu():
            if request.method == "POST":
                comment = request.values.get('valus')
                comment = eval(comment)
                print(comment)
                result = stuAdd(comment)
            return result

        @app.route('/countStu', methods=["POST"])
        def countStu():
            result = stuCount()
            print(result)
            return result

        @app.route('/Countstatics',methods=["POST"])
        def Countstatics():
            if searchUserId(request.headers.get("userid")) :
                return staticsCount()
            else:
                return returnData({}, "请先登录", 401)
